package com.Main.faislamovers;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.MarshalBase64;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.Classes.ViewReservation;
import com.Util.ViewReservationAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class ViewReservationAdp extends Activity {

	String NAMESPACE = "http://tempuri.org/";
	String METHODNAME = "ViewReservation";
	String url = "http://10.0.2.2:53512/Service1.asmx";
	String SopaAction = "http://tempuri.org/ViewReservation";
	static String INFO_TAG = "[FM_INFO]";
	ProgressDialog dialog;
	ProgressBar progressBar;

	public ArrayList<ViewReservation> myResLIst = new ArrayList<ViewReservation>();

	ListView listview;
	ViewReservationAdapter adapter;
	
	static String result22;
	static String source_new, destination_new;
	boolean isFromBooking;
	String user_name, phone_no;

	
	@Override
	protected void onRestart() {
		super.onRestart();
		Toast.makeText(getBaseContext(), "List view is updated", Toast.LENGTH_LONG).show();
		
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		// myOrderLIst = (ArrayList<Order>)
		// Collections.synchronizedList(com.Main.faislamovers.Login.myOrderLIst);

		super.onCreate(savedInstanceState);
		setContentView(R.layout.find_busses);
		user_name = getIntent().getStringExtra(AppConstants.USER_NAME);
		phone_no = getIntent().getStringExtra(AppConstants.PHONE_NO);
		listview = (ListView) findViewById(R.id.mainListView);
		// Login.myOrderLIst = new ArrayList<Order>();

		adapter = new ViewReservationAdapter(this,
				R.layout.viewreservationlistview, myResLIst);

		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (!isFromBooking) {
					
					Intent reservIntent = new Intent(ViewReservationAdp.this,
							GetTicketDetail.class);
					reservIntent.putExtra(AppConstants.SOURCE, myResLIst.get(position).route_from);
					reservIntent.putExtra(AppConstants.DESTINATION, myResLIst.get(position).route_to);
					reservIntent.putExtra(AppConstants.DATE, myResLIst.get(position).travelDate);
					reservIntent.putExtra(AppConstants.ARRIVAL_TIME, myResLIst.get(position).travelArrivalTime);
					reservIntent.putExtra(AppConstants.DEPARTURE_TIME, myResLIst.get(position).travelDepatureTime);
					reservIntent.putExtra(AppConstants.TRAVEL_ID, myResLIst.get(position).travel_id);
					reservIntent.putExtra(AppConstants.SEAT_NO, myResLIst.get(position).seat_number);
					startActivity(reservIntent);

				}
			}
		});
		dialog = new ProgressDialog(ViewReservationAdp.this);
		dialog.setMessage("Loading...");
		dialog.show();

		new Thread(new Runnable() {
			public void run() {
				SoapObject request = new SoapObject(NAMESPACE, METHODNAME);
				request.addProperty("mobile_number", phone_no);
				request.addProperty("user_name", user_name);

				SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
						SoapEnvelope.VER11);
				envelope.dotNet = true;
				MarshalBase64 m = new MarshalBase64();
				m.register(envelope);
				envelope.encodingStyle = SoapEnvelope.ENC;
				// send request
				envelope.setOutputSoapObject(request);
				HttpTransportSE androidHttpTransport = new HttpTransportSE(url);
				try {
					androidHttpTransport.call(SopaAction, envelope);
				} catch (IOException e) {
				
					e.printStackTrace();
				} catch (XmlPullParserException e) {
				
					e.printStackTrace();
				}

				String rsult = null;
				try {
					rsult = envelope.getResponse().toString();
				} catch (SoapFault e) {
					
					e.printStackTrace();
				}
				// Toast.makeText(MyBooking.this, rsult,
				// Toast.LENGTH_LONG).show();
				parseJSON(rsult);

				runOnUiThread(new Runnable() {

					@Override
					public void run() {

						listview.setAdapter(adapter);
						if (dialog.isShowing())
							dialog.hide();
					}
				});
			}
		}).start();
	}

		private void addProgressBar() {
			progressBar = new ProgressBar(this, null,
					android.R.attr.progressBarStyleSmall);
			this.addContentView(progressBar, new LayoutParams(50, 50));
			// progressBar.startAnimation(null);
		}
		/*
		 * Order order; for(int i =0; i < 10; i++) { order = new Order();
		 * 
		 * order.route_from = source_new; order.route_to = destination_new;
		 * order.bus_seats = "50"; order.travelArrivalTime = result22;
		 * order.travelDepatureTime = "12:00 PM"; order.bus_type = "A/C";
		 * order.price= "1000"; order.Currency="Rs:";
		 * Login.myOrderLIst.add(order); }
		 * 
		 * adapter.notifyDataSetChanged();
		 */

	

	public void parseJSON(String json) {

		Type colcitonType = new TypeToken<ArrayList<ViewReservation>>() {
		}.getType();

		List<ViewReservation> list = new Gson().fromJson(json, colcitonType);

		for (ViewReservation o : list) {
			// o.Time = parseTime(o.Time);
			myResLIst.add(o);
		}
	}

	private class FetchOrdersAsyncTask extends AsyncTask<Void, Void, Void> {
		ProgressDialog dialog;
		Context ctx;

		public FetchOrdersAsyncTask(Context contxt) {
			this.ctx = contxt;
		}

		@Override
		protected void onPreExecute() {

			dialog = new ProgressDialog(this.ctx);
			dialog.setMessage("Loading...");
			if (!dialog.isShowing())
				dialog.show();

			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(Void... params) {

			return null;
		}

		@Override
		protected void onPostExecute(Void result) {

			super.onPostExecute(result);
		}

	}
}
